<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\logic\AddressLogic;
use app\common\model\Area;
use think\Db;
use think\Request;


/**
 * Token接口
 */
class Address extends Api
{
    protected $noNeedLogin = ['getCity', 'createJson', 'getCodeArea'];
    protected $noNeedRight = '*';
    protected $addressLogic = null;
    protected $beforeActionList = [
        'areaChange'  =>  ['only'=>'add'],
    ];

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->addressLogic = new AddressLogic();
    }

    protected function areaChange() {
        $param = $this->request->post();
        $province = $param['province'];
        $city = $param['city'];
        $area = $param['area'];
        if ($province) {
            $province = Db::name('area')->where(['level'=>1, 'extra'=> $province])->value('id');
            $this->request->post(['province' => $province]);
        }
        if ($city) {
            $city = Db::name('area')->where(['level'=>2, 'extra'=> $city])->value('id');
            $this->request->post(['city' => $city]);

        }
        if ($area) {
            $area = Db::name('area')->where(['level'=>3, 'extra'=> $area])->value('id');
            $this->request->post(['area' => $area]);
        }


    }

    /**编辑修改*/
    public function add() {
        $this->addressLogic->add();
        $this->success('保存成功');
    }

    /**删除地址*/
    public function del() {
        $id = input('id/d', 0);
        $this->addressLogic->del($id);

        $this->success('删除成功');
    }


    public function listAddress() {
        $list = $this->addressLogic->addressList();

        $this->success('地址列表', $list);
    }

    public function getDefaultAddress() {
        $info = $this->addressLogic->getDefault();

        $this->success('默认地址', $info);
    }

    /**
     * 根据经纬度获取城市
     * $lat, $lng
     */
    public function getCity() {
        $lat = input('lat/s', '');//维度
        $lng = input('lng/s', '');//经度
        $cityData = $this->addressLogic->txCity($lat, $lng);
        if ($cityData === false) {
            $this->error('获取位置信息失败');
        }
        $this->success('城市', $cityData);
    }

   public function createJson() {
        $area = new Area();
        $porvince = $area->where(['level' => 1])->field(['id', 'name'])->select();
        $porvince = collection($porvince)->toArray();
        $province = [];
        $city = [];
        $country = [];
        foreach ($porvince as $k => &$v) {
            $v1code = (string)sprintf("%02d", $k+11) ;
            $v['code'] = $v1code . '0000';
           // array_push($province, $v);
           // $province[$v['code']] = $v['name'];
            $area->where(['id' => $v['id']])->setField('extra', $v['code']);
            $cityData = $area->where(['pid' => $v['id']])->field(['id', 'name'])->select();//城市
            foreach ($cityData as $k2 => &$v2) {
                $v2code = $v1code . sprintf("%02d", $k2+1);
                $v2['code'] = $v2code . '00';
                //array_push($city, $v2->toArray());
                //$city[$v2['code']] = $v2['name'];
                $area->where(['id' => $v2['id']])->setField('extra', $v2['code']);

                $areaData = $area->where(['pid' => $v2['id']])->field(['id', 'name'])->select();
                foreach ($areaData as $k3 => $v3) {
                    $v3['code'] = $v2code . sprintf("%02d", $k3+1);
                   // array_push($country, $v3->toArray());
                    //$country[$v3['code']] = $v3['name'];
                    $area->where(['id' => $v3['id']])->setField('extra', $v3['code']);

                }
            }
        }
        $this->success('ffff');
        /*file_put_contents(
            APP_PATH . 'extra' . DS . 'area_code.php',
            '<?php' . "\n\nreturn " . var_export([
                'province_list' => $province,
                'city_list' => $city,
                'country_list' => $country
            ], true) . ";"
        );*/
    }

    public function getCodeArea() {
        $this->success('地区', config('area_code'));
    }

}
